import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/notice_dialog.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/vo/activities_list_vo.dart';
import 'package:xinxuan/vo/banner_list_vo.dart';
import 'package:xinxuan/vo/blindbox/blindbox_activity_vo.dart';
import 'package:xinxuan/vo/categoires_list_vo.dart';
import 'package:xinxuan/vo/common_vo/common_news_item_vo.dart';
import 'package:xinxuan/vo/features_list_vo.dart';
import 'package:xinxuan/vo/news_list_vo.dart';

class HomePageController extends GetxController {

  final CommonPageController commonPageController = Get.find();

  /// 顶部导航栏下边
  var tabIndex = 0.obs;
  /// 分类列表 List<List<CategoiresItem>>
  var categoiresArr = [].obs;
  /// banner 列表
  var bannerArr = <String>[].obs;
  /// 活动列表
  var activitiesArr = <ActivitiesItem>[].obs;
  /// 精选好物
  var featuresListVo = FeaturesListVo(data: [], meta: Meta(image: '')).obs;
  int featuresPage = 1;
  /// 首页公告
  var newsArr = <CommonNewsItemVo>[].obs;
  /// 盲盒大礼包
  var blindboxActivityData = BlindboxActivityData(id: 0, title: '', image: '', price: '0.00', sales: 0).obs;

  List<Map<String, dynamic>> goodThingTypeArr = [
    {
      'title': '食品名酒',
      'id': 16
    },
    {
      'title': '女装',
      'id': 20
    },
    {
      'title': '相机数码',
      'id': 19
    }
  ];

  /// 商品分类下标
  var goodThingTypeIndex = 0.obs;

  initPageDate() {
    EasyLoading.show();
    Future.wait([
      getCategoiresList(),
      getBannersList(),
      getActivitiesList(),
      getFeaturesList(),
      getNewsList(),
      getBlindboxActivity()
    ]).then((results){
      categoiresArr.value = results[0] as List<List<CategoiresItem>>;
      List<BannerItem> bannerList = results[1] as List<BannerItem>;
      bannerArr.value = [];
      bannerList.forEach((item) {
        bannerArr.add(item.image!);
      });
      activitiesArr.value = results[2] as List<ActivitiesItem>;
      // featuresListVo.value = results[3] as FeaturesListVo;
      newsArr.value = results[4] as List<CommonNewsItemVo>;
      blindboxActivityData.value = results[5] as BlindboxActivityData;
      update();
      EasyLoading.dismiss();
      // showNoticeDialog();
    }).catchError((e){
      EasyLoading.dismiss();
    });
  }

  @override
  void onInit() {
    initPageDate();
    super.onInit();
  }

  /// 获取分类
  Future<List<List<CategoiresItem>>> getCategoiresList() async {
    CategoiresListVo categoiresListVo = await HttpUtilService.getCategoiresList(loading: false);
    List<List<CategoiresItem>> pageArr = [];
    List<CategoiresItem> itemArr = [];
    categoiresListVo.data!.forEach((item) {
      if(itemArr.length == 8) {
        itemArr=[];
      }
      if(itemArr.length == 0) {
        pageArr.add(itemArr);
      }
      itemArr.add(item);
    });
    return pageArr;
  }

  /// 获取banner 列表
  Future<List<BannerItem>> getBannersList() async {
    BannerListVo bannerListVo = await HttpUtilService.getBannersList(loading: false);
    return bannerListVo.data!;
  }
  
  /// 获取活动列表
  Future<List<ActivitiesItem>> getActivitiesList() async {
    ActivitiesListVo activitiesListVo = await HttpUtilService.getActivitiesList(loading: false);
    return activitiesListVo.data!;
  }

  /// 获取首页精选好物
  Future<FeaturesListVo> getFeaturesList({bool loading = false}) async {
    featuresPage = 1;
    FeaturesListVo tempVo = await HttpUtilService.getFeaturesList(typeId: goodThingTypeArr[goodThingTypeIndex.value]['id'], page: featuresPage, loading: loading);
    featuresListVo.value = tempVo;
    update();
    return tempVo;
  }

  /// 获取更多精选好物
  onloadFeaturesList() async {
    if (featuresListVo.value.meta!.pagination!.total! <= featuresListVo.value.data!.length) {
      return;
    }
    featuresPage++;
    FeaturesListVo tempVo = await HttpUtilService.getFeaturesList(typeId: goodThingTypeArr[goodThingTypeIndex.value]['id'], page: featuresPage, loading: false);
    featuresListVo.value.data!.addAll(tempVo.data!);
    update();
  }

  /// 获取首页公告
  Future<List<CommonNewsItemVo>> getNewsList() async {
    NewsListVo newsListVo = await HttpUtilService.getNewsList(loading: false);
    return newsListVo.data!;
  }

  /// 获取活动规则
  Future<BlindboxActivityData> getBlindboxActivity() async {
    BlindboxActivitiesVo blindboxActivitiesVo = await HttpUtilService.getBlindboxActivity(loading: false);
    return blindboxActivitiesVo.data!;
  }

  /// 展示通知详情
  clickNoticeItem({required String title, required String description}) {
    Get.dialog(
      NoticeDialog(
        title: '$title',
        description: '$description',
      ),
      barrierDismissible: false
    );
  }
  
}